package com.yogurt.mp.utils;

import jakarta.validation.ValidationException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class FacadeAspect {

    @Around("@annotation(com.yogurt.mp.utils.Facade)")
    public Object around(ProceedingJoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        System.out.println("执行的方法是: " + methodSignature.getMethod());
        Class returnType = methodSignature.getReturnType();
        System.out.println("返回值类型是: " + returnType);
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            try {
                BeanValidator.validateObject(arg);
            }catch (ValidationException e) {
                System.out.println(e.getMessage());
            }
        }

        Object proceed = null;
        try {
            proceed = joinPoint.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        return proceed;
    }
}
